package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.BigDataModel;
import com.jp.knowledge.util.ImageLoadUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/8.
 */

public class InfoDataDetailBigDataTab extends LinearLayout implements View.OnClickListener {

    public InfoDataDetailBigDataTab(Context context) {
        super(context);
        init();
    }

    public InfoDataDetailBigDataTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public InfoDataDetailBigDataTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for(int i = 0, j = getChildCount(); i < j; i++)
            getChildAt(i).getLayoutParams().width = getMeasuredWidth()/j;
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    public void onClick(View view) {
        if(view == null || getTag() == view)
            return;
        View selectItem = (View) getTag();
        if(selectItem != null){
            selectItem.setBackgroundColor(0);
            ((TextView)view.findViewById(R.id.big_data_title)).setTextColor(getResources().getColor(R.color.font_gray));
        }
        view.setBackgroundColor(getResources().getColor(R.color.bg_color_layout));
        ((TextView)view.findViewById(R.id.big_data_title)).setTextColor(getResources().getColor(R.color.theme_color));
        setTag(view);
        if(this.tabItemSelectedListener != null)
            this.tabItemSelectedListener.onTabItemSelected(indexOfChild(view));
    }

    public void setSelectedItem(int position){
        onClick(getChildAt(position));
    }

    public void initItem(List<BigDataModel.CateListBean> datas){
        removeAllItems();
        if(datas == null)
            return;
        for(BigDataModel.CateListBean data : datas)
            addItem(data);
    }

    public void addItem(BigDataModel.CateListBean data){
        if(data == null)
            return;
        View itemView = View.inflate(getContext(), R.layout.activity_info_data_detail_bigdata_tab_item, null);
        ImageView bigDataIcon = (ImageView) itemView.findViewById(R.id.big_data_item_icon);
        TextView bigDataTitle = (TextView) itemView.findViewById(R.id.big_data_title);
        ImageLoadUtil.load(getContext(), data.getIcon(), bigDataIcon);
        bigDataTitle.setText(data.getName() == null ? "" : data.getName());
        itemView.setTag(data);
        addView(itemView);
        itemView.setOnClickListener(this);
//        for(int i = 0; i < getChildCount(); i++)
//            getChildAt(i).getLayoutParams().width = getResources().getDisplayMetrics().widthPixels / getChildCount();
    }

    public void removeItem(int position){
        if(getTag() == getChildAt(position))
            setTag(null);
        removeViewAt(position);
    }

    public void removeAllItems(){
        setTag(null);
        removeAllViews();
    }

    public int getClickItem(){
        try{
            return indexOfChild((View) getTag());
        } catch (Exception e) {
            return -1;
        }
    }

    public int getItemCount(){
        return getChildCount();
    }

    private TabItemSelectedListener tabItemSelectedListener;
    public void setOnTabItemClickListener(TabItemSelectedListener listener){
        this.tabItemSelectedListener = listener;
    }
    public interface TabItemSelectedListener {
        void onTabItemSelected(int position);
    }
}
